export const genCrudXMeta = function(options={}, adjustOptions) {
  return genWebCrud(options, adjustOptions)
}

export const genCrudComponentsT_T = function(optionsLeft={}, optionsRight={}, adjustOptionsLeft, adjustOptionsRight) {
  let xcrudType = optionsLeft.page.xmeta._xcrud.type
  let isList = xcrudType === 'list'
  let xcrudArea = optionsLeft.query._xcrudArea

  if(isList) {
    let ret = []
    if(optionsLeft.hasListHeader!==false) {
      ret.push({
        xcode: 'header',
      })
    }
    let xcomponentsLeft = genWebCrud({
      ...optionsLeft,
      hasListHeader: false,
    }, adjustOptionsLeft)
    let xcomponentsRight = genWebCrud({
      ...optionsRight,
      hasListHeader: false,
    }, adjustOptionsRight)
    ret.push({
      xcode: 'View',
      xclass: 'flex-start t-mar-top ',
      xcomponents: [
        {
          xcode: 'View',
          xclass: 't-pad-base flex-1 t-bg-white t-mar-right-base',
          xcomponents: xcomponentsLeft,
        },
        {
          xcode: 'View',
          xclass: 't-pad-base flex-1 t-bg-white t-mar-right-base',
          xcomponents: xcomponentsRight,
        }
      ]
    })
    return ret
  }else if(xcrudArea==='left'){
    return genWebCrud(optionsLeft, adjustOptionsLeft)
  }else if(xcrudArea==='right'){
    return genWebCrud(optionsRight, adjustOptionsRight)
  }else{
    return []
  }
}

export const genWebCrud = function(options={}, adjustOptions) {
  let xcrudType = options.page._xcrudGetType()||'list'
  let isList = xcrudType === 'list'
  let page = options.page
  let pageQuery = options.page.query

  // -------------
  // 参数
  options.header = options.header||{}
  let header = options.header
  header.btns = header.btns||[]
  options.query = options.query||{}
  let query = options.query
  query.btns = []
  options.xfields = options.xfields||[]
  let xfields = options.xfields
  let queryFields = []
  let listFields = []
  let detailFields = []
  options.queryFields = queryFields
  options.listFields = listFields
  options.detailFields = detailFields
  let listItemOperation = options.listItemOperation||{}
  listItemOperation.btns = listItemOperation.btns||[]

  // header 按钮
  if(isList&&header.canCreate!==false){
    header.btns.push({
      type: 'primary',
      size: 'small',
      xtitle: '新增',
      onClick: ()=>page._xcrudToCreate(),
    })
  }
  if(!isList&&header.canEdit!==false){
    header.btns = header.btns.concat([
      {
        xif: ()=>!options.page._xcrudGetEditing(),
        type: 'primary',
        size: 'small',
        xtitle: '编辑',
        onClick: ()=>options.page._xcrudSetEditing(true)
      },
      {
        xif: ()=>options.page._xcrudGetEditing(),
        type: 'primary',
        size: 'small',
        xtitle: '保存',
        onClick: ()=>{
          options.page._xcrudDoNetwork(xcrudType).then(()=>{
            options.page._xcrudSetEditing(false)
          })
        }
      },
      {
        xif: ()=>options.page._xcrudGetEditing(),
        type: 'primary',
        size: 'small',
        xtitle: '取消',
        onClick: ()=>options.page._xcrudSetEditing(false)
      },
    ])
  }

  // 字段
  xfields.forEach(v=>{
    // 搜搜
    if(isList&&v._canQuery) {
      if(!v.placeholder&&v.placeholder!==false) {
        if(v.type==='daterange') {
          v.placeholder = v.xtitle + '起'
          v.placeholder1 = v.xtitle + '止'
        }else{
          v.placeholder = v.xtitle
        }
      }
      queryFields.push({
        ...v,
        xtitle: v.xtitleQuery||v.xtitle,
        xfield: v.xfieldQuery||v.xfield,
        xspan: v.xspanQuery||v.xspan
      })
    }
    // 列表
    if(isList&&v._canList!==false) {
      listFields.push({
        ...v,
        xtitle: v.xtitleList||v.xtitle,
        xfield: v.xfieldList||v.xfield,
      })
    }
    // 增改详类型
    if(xcrudType==='create'&&v._canCreate!==false) {
      detailFields.push({
        ...v,
        xtitle: v.xtitleCreate||v.xtitle,
        xfield: v.xfieldCreate||v.xfield,
        xspan: v.xspanCreate||v.xspan
      })
    }
    if(xcrudType==='update'&&v._canUpdate!==false) {
      detailFields.push({
        ...v,
        xtitle: v.xtitleUpdate||v.xtitle,
        xfield: v.xfieldUpdate||v.xfield,
        xspan: v.xspanUpdate||v.xspan
      })
    }
    if(xcrudType==='detail'&&v._canDetail!==false) {
      detailFields.push({
        ...v,
        xtitle: v.xtitleDetail||v.xtitle,
        xfield: v.xfieldDetail||v.xfield,
        xspan: v.xspanDetail||v.xspan
      })
    }
  })
  if(queryFields.length){
    queryFields.push({
      xspan: 1,
      xtype: 'submit',
      onClick: ()=>options.page._xcrudDoRefresh(),
    })
  }
  

  // 表格操作列
  if(listItemOperation.enable!==false) {
    if(listItemOperation.canDetail===true) {
      listItemOperation.btns.push({
        xtitle: '详情',
        onClick: (x,data={})=>{},
      })
    }
    if(listItemOperation.canUpdate!==false) {
      listItemOperation.btns.push({
        xtitle: '修改',
        onClick: (x,data={})=>page._xcrudToUpdate(data)
      })
    }
    if(listItemOperation.canSub===true) {
      listItemOperation.btns.push({
        xtitle: '子项',
        onClick: (x,data={})=>options.page._xcrudToSub(data),
      })
    }
    if(listItemOperation.canDelete!==false) {
      listItemOperation.btns.push({
        xtitle: '删除',
        onClick: (x, data={})=>page._xcrudDoDelete(data),
      })
    }
    options.listFields.push({
      xtitle: '操作',
      width: listItemOperation.width||100,
      type: 'operation',
      btns: listItemOperation.btns
    })
  }

  // -------------
  // 参数回调设置
  if(options.ajust) options.ajust(options)

  // -------------
  // 输出组件
  let xcomponents = []

  // 头部
  if((isList&&header.canList!==false)||(!isList&&options.hasDetailHeader!==false)) {
    xcomponents.push({
      xcode: 'header',
      btns: header.btns
    })
  }

  // query
  if(isList&&query.enable!==false){
    xcomponents.push({
      xcode: 'View',
      xclass: 't-bg-white t-pad-base',
      xcomponents: [
        {
          xcode: 'querys',
          xdatapath: '_xcrud.query',
          xfields: options.queryFields,
          btns: query.btns
        },
      ]
    })
  }

  // table
  if(isList){
    xcomponents.push({
      xcode: 'View',
      xclass: 't-bg-white t-pad-base',
      xcomponents: [
        {
          xcode: 'table',
          xfields: options.listFields,
          xdatapath: '_xcrud.list',
        },
        {
          xcode: 'pagination',
          xdatapath: '_xcrud.paging',
          xfield: 'pageIndex',
          xfield1: 'total',
          xfield2: 'pageSize',
          onClickCurrent: (x, data)=>options.page._xcrudDoMore(data),
        },
      ]
    })
  }

  // detail
  if(!isList) {
    xcomponents.push({
      xcode: 'View',
      xclass: 't-pad-base t-bg-white',
      xcomponents: [
        {
          xcode: 'details',
          xfields: detailFields,
          xdatapath: '_xcrud.detail',
        }
      ]
    })
  }

  return {
    xfields,
    xcomponents
  }
}